package com.example.nyf.service;

import com.example.nyf.common.Common;
import com.example.nyf.dto.SysRoleDTO;
import com.example.nyf.entity.SysRole;
import com.example.nyf.entity.SysRoleMenu;
import com.example.nyf.mapper.SysRoleMapper;
import com.example.nyf.mapper.SysRoleMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

@Controller
public class SysRoleService {
    @Autowired
    SysRoleMapper sysRoleMapper;

    @Autowired
    SysRoleMenuMapper sysRoleMenuMapper;

    public List<SysRole> getSysRoleList() {
        return sysRoleMapper.getSysRoleList();
    }

    public Set<String> getSysPerByUserId(Integer user_id) {
        return sysRoleMapper.getSysPerByUserId(user_id);
    }

    public Set<String> getSysRoleByUserId(Integer user_id) {
        return sysRoleMapper.getSysRoleByUserId(user_id);
    }

    public SysRole getSysRoleById(Integer id) {
        return sysRoleMapper.getSysRoleById(id);
    }

    public SysRoleDTO getRoleMenuById(Integer id) {
        return sysRoleMapper.getRoleMenuById(id);
    }
    @Transactional
    public void insertSysRole(SysRoleDTO sysRoleDTO) {
        sysRoleMapper.insertSysRole(sysRoleDTO);
        Integer role_id = sysRoleDTO.getId();

        Integer[] menu = sysRoleDTO.getMenu_id_array();
        if(menu!=null && menu.length>0) {
            sysRoleMenuMapper.insertAllSysRoleMenu(menu, role_id);
        }
    }

    @Transactional
    public void updateSysRole(SysRoleDTO sysRoleDTO) {
        Integer role_id = sysRoleDTO.getId();

        // 1.更新sys_role表
        sysRoleMapper.updateSysRole(sysRoleDTO);
        List<SysRoleMenu> list = sysRoleMenuMapper.listSysRoleMenu(role_id);
        Integer[] menu_old = new Integer[list.size()];
        for (int i=0; i<list.size(); i++) {
            menu_old[i] = list.get(i).getMenu_id();
        }
        Integer[] menu_new = sysRoleDTO.getMenu_id_array();

        Integer[] menu_add = Common.getDiffInteger(menu_new, menu_old); // 需添加的菜单
        Integer[] menu_remove = Common.getDiffInteger(menu_old, menu_new); // 需移除的菜单
        // 2.更新sys_role_menu表
        // 新增角色菜单
        SysRoleMenu sysRoleMenu = new SysRoleMenu();
        for(int i=0; i<menu_add.length; i++) {
            sysRoleMenu.setRole_id(role_id);
            sysRoleMenu.setMenu_id(menu_add[i]);
            sysRoleMenuMapper.insertSysRoleMenu(sysRoleMenu);
        }

        //String menu_id = StringUtils.join(menu_remove,",");
        // 数组 转 list
        List<Integer> listMenuRemove =  Arrays.asList(menu_remove);
        // 删除角色菜单
        if(listMenuRemove.size()>0) {
            sysRoleMenuMapper.deleteSysRoleMenu(menu_remove, role_id);
        }

    }

    @Transactional
    public void deleteSysRole(Integer role_id) {
        sysRoleMapper.deleteSysRole(role_id);
        sysRoleMenuMapper.deleteSysRoleById(role_id);
    }
}
